

// webpack.prod.js (生产环境配置)
const { merge } = require('webpack-merge');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const common = require('./webpack.common.js');

const glob = require('glob');
const path = require('path');

module.exports = merge(common, {
    mode: 'production',
    plugins: [
        new PurgeCSSPlugin({
            paths: glob.sync(`${path.resolve(__dirname, 'src')}/**/*`, { nodir: true }),
        }),
        new MiniCssExtractPlugin({
            filename: 'css/[name].[contenthash].css', // 使用哈希值缓存
        }),
    ],

    ptimization: {
        minimize: true,
        minimizer: [
            new TerserPlugin(), // JS 压缩
            new CssMinimizerPlugin(), // CSS 压缩
        ],
        splitChunks: {
            cacheGroups: {
                styles: {
                    name: 'common', // 提取的公共 CSS 文件名
                    type: 'css/mini-extract',
                    chunks: 'all',
                    enforce: true,
                },
            },
        },
    },
});